//
//  BanJiDetailViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BanJiDetailViewController.h"
#import "YYCollectionViewCell.h"

@interface BanJiDetailViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UIButton *teacherBtn;

@property (nonatomic, strong)UIButton *studentBtn;

@property (nonatomic, strong)UIView *leftLineView;

@property (nonatomic, strong)UIView *rightLineView;

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, assign)BOOL isLaoShi;

@property (nonatomic, strong)NSMutableArray *laoshiArray;

@property (nonatomic, strong)NSMutableArray *xueshengArray;

@end

@implementation BanJiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = _titleName;
    
    _isLaoShi = YES;
    
    [self loadData];
    
    [self setUI];

     [self setcollectionView];
    
}

-(void)loadData{
    
    _laoshiArray = [NSMutableArray array];
    _xueshengArray = [NSMutableArray array];
    
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"class_id":_classID
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Class&a=classUserList",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            for (NSDictionary *dic in responseObject[@"data"][@"student_list"]) {
                [_xueshengArray addObject:dic];
            }
            
            for (NSDictionary *dic in responseObject[@"data"][@"teacher_list"]) {
                [_laoshiArray addObject:dic];
            }
            if (Teacher) {
                [_teacherBtn setTitle:[NSString stringWithFormat:@"学生人数(%lu)",(unsigned long)_xueshengArray.count] forState:0];
            }else{
                
                [_teacherBtn setTitle:[NSString stringWithFormat:@"老师(%lu)",(unsigned long)_laoshiArray.count] forState:0];
            }
            
            
            [_studentBtn setTitle:[NSString stringWithFormat:@"学生(%lu)",(unsigned long)_xueshengArray.count] forState:0];
            
            [self.collectionView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}


-(void)setcollectionView{
    //设置布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置item大小
    flowLayout.itemSize = CGSizeMake(50 * KscreenWidth, 80 * KscreenHeight);
    
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 40 * KscreenWidth;
    
    //设置最小行间距
    flowLayout.minimumLineSpacing = 10 * KscreenHeight;
    
    //设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 52 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT - 52 * KscreenHeight) collectionViewLayout:flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    //设置代理
    _collectionView.delegate = self;
    
    //设置数据源
    _collectionView.dataSource = self;
    
    
    //注册
    [_collectionView registerClass:[YYCollectionViewCell class] forCellWithReuseIdentifier:@"YYCollectionViewCell"];
    
    //添加到视图
    [self.view addSubview:_collectionView];
}

-(void)setUI{
    
    _teacherBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _teacherBtn.frame = CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth / 2, 50 * KscreenWidth);
    [_teacherBtn setTitleColor:ZhuTiColor forState:0];
    [_teacherBtn setTitle:@"老师(8)" forState:0];
    [_teacherBtn addTarget:self action:@selector(teacherAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _leftLineView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth / 2 - 80 * KscreenWidth) / 2, NAVIGATION_BAR_HEIGHT + 47 * KscreenHeight, 80 * KscreenWidth, 3 * KscreenHeight)];
    _leftLineView.backgroundColor = ZhuTiColor;
    
    
    _studentBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _studentBtn.frame = CGRectMake(ScreenWidth / 2, NAVIGATION_BAR_HEIGHT, ScreenWidth / 2, 50 * KscreenWidth);
    [_studentBtn setTitleColor:ZhuTiColor forState:0];
    [_studentBtn setTitle:@"学生(39)" forState:0];
    [_studentBtn addTarget:self action:@selector(studentAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    _rightLineView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth / 2 - 80 * KscreenWidth) / 2 + ScreenWidth / 2, NAVIGATION_BAR_HEIGHT + 47 * KscreenHeight, 80 * KscreenWidth, 3 * KscreenHeight)];
    _rightLineView.hidden = YES;
    _rightLineView.backgroundColor = ZhuTiColor;
    
   
    
    UIView *zhongjianline = [[UIView alloc]initWithFrame:CGRectMake(ScreenWidth / 2, 10 * KscreenWidth + NAVIGATION_BAR_HEIGHT, 1 * KscreenWidth, 30 * KscreenHeight)];
    zhongjianline.backgroundColor = hui1Color;
    [self.view addSubview:zhongjianline];
    
    UIView *bottomline = [[UIView alloc]initWithFrame:CGRectMake(0, 50 * KscreenWidth  + NAVIGATION_BAR_HEIGHT, ScreenWidth, 1 * KscreenHeight)];
    bottomline.backgroundColor = hui1Color;
    
    
    if(Teacher){
        [self.view addSubview:_teacherBtn];
        [_teacherBtn setTitle:@"学生人数：人" forState:0];
        [_teacherBtn setTitleColor:[UIColor blackColor] forState:0];
    }else{
        [self.view addSubview:_teacherBtn];
        [self.view addSubview:_leftLineView];
        [self.view addSubview:_studentBtn];
        [self.view addSubview:_rightLineView];
        [self.view addSubview:bottomline];
    }
    
    
    
}

-(void)teacherAction{
    _isLaoShi = YES;
    _leftLineView.hidden = NO;
    _rightLineView.hidden = YES;
    [self.collectionView reloadData];
}

-(void)studentAction{
    _isLaoShi = NO;
    _leftLineView.hidden = YES;
    _rightLineView.hidden = NO;
    [self.collectionView reloadData];
    
}


#pragma mark 必须实现的方法
//每分区有多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (Teacher) {
         return _xueshengArray.count;
    }else{
        if (_isLaoShi == YES) {
            return _laoshiArray.count;
        }else{
            
            return _xueshengArray.count;
        }
        
    }
    
    
}

//每个item上面现实的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //从重用池里面获取cell
    YYCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YYCollectionViewCell" forIndexPath:indexPath];
//防止cell复用带来的控件重叠
//    for (UIView *view in cell.contentView.subviews) {
//        [view removeFromSuperview];
//    }
    
    if (Teacher) {
            cell.nameLable.text = [_xueshengArray[indexPath.row] valueForKey:@"name"];
            [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:[_xueshengArray[indexPath.row] valueForKey:@"small_img"]] placeholderImage:[UIImage imageNamed:@"图层18"]];
        //返回cell
        return cell;
        
    }else{
        
        if (_isLaoShi == YES) {
            cell.nameLable.text = [_laoshiArray[indexPath.row] valueForKey:@"name"];
            [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:[_laoshiArray[indexPath.row] valueForKey:@"small_img"]] placeholderImage:[UIImage imageNamed:@"图层18"]];
        }else{
            
            cell.nameLable.text = [_xueshengArray[indexPath.row] valueForKey:@"name"];
            [cell.headerImgView sd_setImageWithURL:[NSURL URLWithString:[_xueshengArray[indexPath.row] valueForKey:@"small_img"]] placeholderImage:[UIImage imageNamed:@"图层18"]];
        }
        
        //返回cell
        return cell;
        
    }
        

}

//设置分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//点击item触发的事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

//返回分区内边距的上左下右的距离
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(30 * KscreenWidth, 30 *KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
